<template>
  <div class="tags" @click="contextmenuFlag = false">
    <div
        v-if="contextmenuFlag"
        class="tags__contentmenu"
        :style="{ left: contentmenuX + 'px', top: contentmenuY + 'px' }"
    >
      <div class="item" @click="closeOthersTags">关闭其它</div>
      <div class="item" @click="closeAllTags">关闭所有</div>
    </div>
    <div class="tas_box">
      <el-tabs
          v-model="active"
          type="card"
          @contextmenu.native="handleContextmenu"
          @tab-click="openTag"
          @edit="menuTag"
      >
        <el-tab-pane
            :key="index"
            :closable="item.label !== '首页'"
            v-for="(item,index) in tagList"
            :name="item.value"
        >
          <template #label>
            <div style="display: flex; align-items: center">
              <sidebarIcon
                  :icon="item.meta.icon ? item.meta.icon : 'HomeFilled'"
              />
              <span style="margin-left: 5px">{{ item.label }}</span>
            </div>
          </template>
        </el-tab-pane>
      </el-tabs>


    </div>
    <el-dropdown class="tags_menu">
      <el-button type="primary" size="small" class="tags_button">
        更 多
        <i class="iconfont icon-ios-options"></i>
      </el-button>
      <template #dropdown>
        <el-dropdown-item @click.native="closeOthersTags"
        >关闭其它
        </el-dropdown-item
        >
        <el-dropdown-item @click.native="closeAllTags"
        >关闭所有
        </el-dropdown-item
        >
      </template>
    </el-dropdown>
  </div>
</template>
<script>
import {mapGetters, mapState} from "vuex";
import sidebarIcon from "@/page/sidebar/sidebar-icon.vue";

export default {
  name: "tags",
  data() {
    return {
      active: "",
      contentmenuX: "",
      contentmenuY: "",
      contextmenuFlag: false,
    };
  },
  components: {
    sidebarIcon,
  },
  mounted() {
    this.setActive();
  },
  watch: {
    tag(newVal, oldVal) {
      this.setActive();
    },
    contextmenuFlag() {
      window.addEventListener("mousedown", this.watchContextmenu);
    },
  },
  computed: {
    ...mapGetters(["tagWel", "tagList", "tag"]),
    ...mapState({
      showTag: (state) => state.common.showTag,
    }),
    tagLen() {
      return this.tagList.length || 0;
    },
  },
  methods: {
    watchContextmenu(event) {
      if (!this.$el.contains(event.target) || event.button !== 0) {
        this.contextmenuFlag = false;
      }
      window.removeEventListener("mousedown", this.watchContextmenu);
    },
    handleContextmenu(event) {
      event.preventDefault();
      event.stopPropagation();
      this.contentmenuX = event.clientX;
      this.contentmenuY = event.clientY;
      this.contextmenuFlag = true;
    },
    setActive() {
      this.active = this.tag.value;
    },
    menuTag(value, action) {
      if (action === "remove") {
        let {tag, key} = this.findTag(value);
        this.$store.commit("DEL_TAG", tag);
        if (tag.value === this.tag.value) {
          tag = this.tagList[key === 0 ? key : key - 1];
          this.openTag(tag);
        }
      }
    },
    openTag(item) {
      if (item.props) item = item.props;
      let tag;
      if (item.name) {
        tag = this.findTag(item.name).tag;
      } else {
        tag = item;
      }
      this.$router.push({
        path: tag.value,
        query: tag.query,
      });
    },
    closeOthersTags() {
      this.contextmenuFlag = false;
      this.$store.commit("DEL_TAG_OTHER");
    },
    findTag(value) {
      let tag, key;
      this.tagList.map((item, index) => {
        if (item.value === value) {
          tag = item;
          key = index;
        }
      });
      return {tag: tag, key: key};
    },
    closeAllTags() {
      this.contextmenuFlag = false;
      this.$store.commit("DEL_ALL_TAG");
      this.$router.push({
        path: this.$router.$quickRouter.getPath({
          src: this.tagWel.value,
        }),
        query: this.tagWel.query,
      });
    },
  },
};
</script>
